/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_serialize_hessian2_req_body_parse.c              */
/*  NOTE      =                                                    */
/*  DATE      = 2017/01/19 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dubbo/dubbo_serialize_dubbo.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../../src/module/dubbo/config.h"

extern int serialize_hessian2_req_body_parse(Frbyte_Packet *p_packet
                        , L7session_dubbo_private *p_st_private
                        , Frreport_body_data *p_st_body_data);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;

    i_ret = serialize_hessian2_req_body_parse(p_packet, p_st_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;
    char *p_data = NULL;

    p_data = (char *)"\x94";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 1) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dubbo_private *)frmalloc(sizeof(L7session_dubbo_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dubbo_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    i_ret = serialize_hessian2_req_body_parse(p_packet, p_st_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_packet);
    dubbo_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);

    return;

ERROR:
    if(p_packet)
        bytebuff_free(&p_packet);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    return;
}

static void case3(void)
{
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    int i_ret = 0;
    char *p_data = NULL;

    p_data = (char *)"\x05\x32\x2e\x35\x2e\x33\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x05\x30\x2e\x30\x2e\x30\x08\x73\x61\x79\x48\x65\x6c\x6c\x6f\x12\x4c\x6a\x61\x76\x61\x2f\x6c\x61\x6e\x67\x2f\x53\x74\x72\x69\x6e\x67\x3b\x06\x77\x6f\x72\x6c\x64\x30\x48\x04\x70\x61\x74\x68\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x09\x69\x6e\x74\x65\x72\x66\x61\x63\x65\x30\x22\x63\x6f\x6d\x2e\x61\x6c\x69\x62\x61\x62\x61\x2e\x64\x75\x62\x62\x6f\x2e\x64\x65\x6d\x6f\x2e\x44\x65\x6d\x6f\x53\x65\x72\x76\x69\x63\x65\x07\x76\x65\x72\x73\x69\x6f\x6e\x05\x30\x2e\x30\x2e\x30\x5a";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 196) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dubbo_private *)frmalloc(sizeof(L7session_dubbo_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dubbo_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    i_ret = serialize_hessian2_req_body_parse(p_packet, p_st_private, p_st_body_data);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);

    bytebuff_free(&p_packet);
    dubbo_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);

    return;

ERROR:
    if(p_packet)
        bytebuff_free(&p_packet);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    return;
}

int add_suite_serialize_hessian2_req_body_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" serialize_hessian2_req_body_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL || 
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
